package servlet;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import domain.User;
import service.UserService;
import service.impl.UserServiceImpl;
import utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * RegisterServlet实现类
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private UserService userService = new UserServiceImpl();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPut(request, response);
    }

    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

        String rs = "";
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkPassword = request.getParameter("ckpassword");
        String email = request.getParameter("email");
        String unit = request.getParameter("unit");
        String registerTime = formatter.format(date);
        System.out.println(registerTime);

        if (StringUtil.isEmpty(username) || StringUtil.isEmpty(password) || StringUtil.isEmpty(checkPassword) || StringUtil.isEmpty(email) || StringUtil.isEmpty(unit)) {
            rs = "注册信息填写不完整！";
        }
        // 两次输入密码是否一致
        if (!password.equals(checkPassword)) {
            rs = "两次密码输入不一致！";
        }

        boolean IsUserExist = userService.checkUsername(username, email);
        if (IsUserExist) {
            rs = "账号已存在！";
        }

        if (StringUtil.isEmpty(rs)) {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setEmail(email);
            user.setUnit(unit);
            user.setRegistertime(registerTime);

            System.out.println(user.getRegistertime());

            UserDao userDao = new UserDaoImpl();
            userDao.register(user);
            System.out.println("注册成功");
            session.setAttribute("usernamePrepared", username);
            request.getRequestDispatcher("register_success.jsp").forward(request, response);
        } else {
            System.out.println("注册失败" + rs);
            request.setAttribute("message", "注册失败！" + rs);
            request.getRequestDispatcher("/register_fail.jsp").forward(request, response);
        }
    }


}